/*     */ package com.zimbra.cs.zimlet;
/*     */ 
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.ElementFactory;
/*     */ import com.zimbra.common.soap.Element.JSONElement;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.common.soap.W3cDomUtil;
/*     */ import com.zimbra.common.soap.XmlParseException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.IOException;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class ZimletMeta
/*     */ {
/*     */   protected Element mTopElement;
/*     */   protected String mName;
/*     */   protected Version mVersion;
/*     */   protected String mDescription;
/*     */   protected boolean mIsExtension;
/*     */   protected String mRawXML;
/*     */   protected String mGeneratedXML;
/*     */   
/*     */   protected ZimletMeta() {}
/*     */   
/*     */   public ZimletMeta(File f)
/*     */     throws ZimletException
/*     */   {
/*  54 */     this(readFile(f));
/*     */   }
/*     */   
/*     */   public ZimletMeta(String meta) throws ZimletException {
/*  58 */     initialize();
/*  59 */     if (meta == null) {
/*  60 */       return;
/*     */     }
/*     */     try {
/*  63 */       this.mTopElement = Element.parseXML(meta);
/*  64 */       this.mRawXML = meta;
/*     */     } catch (XmlParseException de) {
/*  66 */       throw ZimletException.INVALID_ZIMLET_DESCRIPTION("Cannot parse Zimlet description: " + de.getMessage());
/*     */     }
/*     */     
/*  69 */     validate();
/*     */   }
/*     */   
/*     */   private static String readFile(File f) throws ZimletException {
/*     */     try {
/*  74 */       return new String(ByteUtil.getContent(new FileInputStream(f), -1));
/*     */     } catch (IOException ie) {
/*  76 */       throw ZimletException.INVALID_ZIMLET_DESCRIPTION("Cannot find Zimlet description file: " + f.getName());
/*     */     }
/*     */   }
/*     */   
/*     */   protected void validate() throws ZimletException {
/*  81 */     if (this.mTopElement == null) {
/*  82 */       throw ZimletException.INVALID_ZIMLET_DESCRIPTION("Null DOM element");
/*     */     }
/*  84 */     String name = this.mTopElement.getName();
/*  85 */     if ((!name.equals("zimlet")) && (!name.equals("zimletConfig"))) {
/*  86 */       throw ZimletException.INVALID_ZIMLET_DESCRIPTION("Top level tag not recognized " + name);
/*     */     }
/*     */     
/*  89 */     this.mName = this.mTopElement.getAttribute("name", "");
/*  90 */     this.mVersion = new Version(this.mTopElement.getAttribute("version", ""));
/*  91 */     this.mDescription = this.mTopElement.getAttribute("description", "");
/*     */     try {
/*  93 */       this.mIsExtension = this.mTopElement.getAttributeBool("extension", false);
/*     */     } catch (Exception se) {
/*  95 */       this.mIsExtension = false;
/*     */     }
/*     */     
/*     */ 
/*  99 */     Iterator iter = this.mTopElement.listElements().iterator();
/* 100 */     while (iter.hasNext()) {
/* 101 */       validateElement((Element)iter.next());
/*     */     }
/*     */   }
/*     */   
/*     */   public String getName() {
/* 106 */     assert (this.mTopElement != null);
/* 107 */     return this.mName;
/*     */   }
/*     */   
/*     */   public Version getVersion() {
/* 111 */     assert (this.mTopElement != null);
/* 112 */     return this.mVersion;
/*     */   }
/*     */   
/*     */   public String getDescription() {
/* 116 */     assert (this.mTopElement != null);
/* 117 */     return this.mDescription;
/*     */   }
/*     */   
/*     */   public boolean isExtension() {
/* 121 */     assert (this.mTopElement != null);
/* 122 */     return this.mIsExtension;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public String toJSONString()
/*     */   {
/* 129 */     return toString(Element.JSONElement.mFactory);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public String toXMLString()
/*     */   {
/* 136 */     return toString(Element.XMLElement.mFactory);
/*     */   }
/*     */   
/*     */   public String getRawXML() {
/* 140 */     return this.mRawXML;
/*     */   }
/*     */   
/*     */ 
/*     */   public String toString(Element.ElementFactory f)
/*     */   {
/*     */     try
/*     */     {
/* 148 */       if (this.mGeneratedXML == null) {
/* 149 */         this.mGeneratedXML = this.mTopElement.toString();
/*     */       }
/* 151 */       return W3cDomUtil.parseXML(this.mGeneratedXML, f).toString();
/*     */     } catch (XmlParseException e) {
/* 153 */       ZimbraLog.zimlet.warn("error parsing the Zimlet file " + this.mName);
/*     */     }
/* 155 */     return "";
/*     */   }
/*     */   
/*     */ 
/*     */   public void addToElement(Element elem)
/*     */     throws ZimletException
/*     */   {
/*     */     try
/*     */     {
/* 164 */       Element newElem = W3cDomUtil.parseXML(toXMLString(), elem.getFactory());
/* 165 */       elem.addElement(newElem);
/*     */     } catch (XmlParseException de) {
/* 167 */       throw ZimletException.ZIMLET_HANDLER_ERROR("cannot parse the dom tree");
/*     */     }
/*     */   }
/*     */   
/*     */   protected abstract void initialize()
/*     */     throws ZimletException;
/*     */   
/*     */   protected abstract void validateElement(Element paramElement)
/*     */     throws ZimletException;
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/zimlet/ZimletMeta.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */